/********************************************************************************
 * MailingSystemServiceDelegator.java
 *
 * Created by Jigar Mistry
 * Last modified Date: Sep 8, 2008 9:49:45 AM by Jigar Mistry
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ********************************************************************************/

package com.printeverywhere.delegator;

import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.printeverywhere.dto.MailingSystem;
import com.printeverywhere.util.HibernateUtil;

/**
 * The Class MailingSystemServiceDelegator.
 */
public class MailingSystemServiceDelegator {
	
	/** The log. */
	private static Logger log = Logger.getLogger(MailingSystemServiceDelegator.class);

	/**
	 * Gets the mailing system.
	 * 
	 * @return the mailing system
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	@SuppressWarnings("unchecked")
	public static MailingSystem getMailingSystem() throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Getting list for MailingSystem without search criteria".intern());
		}
		Session session = HibernateUtil.getSession();
		try {
			Transaction transaction = session.beginTransaction();
			List<MailingSystem> mailingSystemList = session.createCriteria(MailingSystem.class).list();
			transaction.commit();
			return (mailingSystemList.size() > 0) ? mailingSystemList.get(0) : new MailingSystem();
		} catch (HibernateException e) {
			log.error("Got exception when getting list for MailingSystem without search criteria".intern());
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info("Closing session".intern());
			}
			session.flush();
			session.close();
		}
	}

	/**
	 * Update mailing system.
	 * 
	 * @param mailingSystem the mailing system
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	public static void updateMailingSystem(MailingSystem mailingSystem) throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Called updateMailingSystem method".intern());
		}
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(mailingSystem);
			tx.commit();
		} catch (RuntimeException e) {
			log.error("Got exception when update MailingSystem without search criteria".intern());
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info("Closing session".intern());
			}
			session.flush();
			session.close();
		}
	}
}